//潜在客户
$(function () {
    var client_datagrid = $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var moveToPool = $("#moveToPool");//移入资源池
    var exchange_dialog = $("#exchange_dialog");//移交客户弹出框
    var exchange_form = $("#exchange_form");//移交客户填的数据
    var developFailed_dialog = $("#developFailed_dialog");//标记开发客户弹出框
    var developFailed_form = $("#developFailed_form");//标记开发失败客户填的数据
    var lost_dialog = $("#lost_dialog");//标记客户流失弹出框
    var lost_form = $("#lost_form");//标记客户流失填的数据


// 数据 表
    client_datagrid.datagrid({
        url: "/client/query.do?&clientState=0",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        rownumbers: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#client_toolbar",
        columns: [
            [
                {field: 'id', hidden: true},
                {field: 'state', hidden: true},//客户状态
                {field: 'inputdate', title: '创建日期', width: 80, sortable: true},
                {
                    field: 'inputuser', title: '创建人', width: 50,
                    formatter: function (value) {
                        return value ? value.username : "";
                    }
                },
                {
                    field: 'chargeuser', title: '负责人', width: 50,
                    formatter: function (value) {
                        return value ? value.username : "";
                    }
                },
                {field: 'name', title: '客户', width: 50},
                {field: 'age', title: '年龄', width: 50},
                {
                    field: 'gender', title: '性别', width: 30,
                    formatter: function (value, row, index) {
                        return value ? "男" : "女";
                    }
                },
                {
                    field: 'job', title: '职业', width: 50
                },
                {field: 'salary', title: '薪资水平', width: 80},
                {field: 'tel', title: '电话', width: 80},
                {field: 'email', title: '邮箱', width: 80},
                {field: 'address', title: '客户地址', width: 80},
                {field: 'source', title: '客户来源', width: 80},
                {field: 'qq', title: 'QQ', width: 80,formatter:function (value) {
                        return '<a href="http://wpa.qq.com/msgrd?v=3&uin='+value+'&site=qq&menu=yes">'+
                            '<font style="color: green">'+
                            value+'</a>'
                    }},
                {field: 'wechat', title: '微信', width: 80},
                {field: 'identitynum', title: '身份证号', width: 80},
            ]
        ],
        onSelect: function (index, row) {
            $("#develop").linkbutton(row.chargeuser ? "disable" : 'enable');
            $("#exchange").linkbutton(row.chargeuser ? "enable" : 'disable');
        }
    });


    // 对话 框
    client_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#form_btns",
        closed: true,
        onClose: function () {
            client_form.form("clear");
        }
    });
    //移交客户对话框
    exchange_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#exchange_btns",
        closed: true,
        onClose: function () {
            exchange_form.form("clear");
        }
    });

    //标记开发失败对话框
    developFailed_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#developFailed_btns",
        closed: true,
        onClose: function () {
            developFailed_form.form("clear");
        }
    });

    //标记客户流失对话框
    lost_dialog.dialog({
        width: 320,
        height: 420,
        top: 100,
        buttons: "#lost_btns",
        closed: true,
        onClose: function () {
            lost_form.form("clear");
        }
    });

    function openSetTitle(str) {
        client_dialog.dialog("setTitle", str);
        client_dialog.dialog("open");
    }

    var objMethod = {
        add: function () {
            $(".myPassword").show();
            openSetTitle("新增客户")
        },

        edit: function () {
            $(".myPassword").hide();
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            if (row.dept) {
                row["dept.id"] = row.dept.id
            }
            // 回显数据
            client_form.form("load", row);

            openSetTitle("编辑客户");
        },
        changeState: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            $.messager.confirm('确认', '你确认要操作吗？', function (r) {
                if (!r) return
                $.get('/client/changeState.do', {id: row.id}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", '操作成功', 'info', function () {
                            client_datagrid.datagrid("reload");  // 刷新
                        })
                    } else {
                        $.messager.alert("温馨提示", '操作失败', 'info', function () {
                        })
                    }
                })
            })
        },
        save: function () {
            // 提交表单
            client_form.form("submit", {
                url: "/client/saveOrUpdate.do",
                onSubmit: function (params) {
                    return client_form.form('validate');
                },
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert("温馨提示", '保存成功', 'info', function () {
                            objMethod.cancel();
                            client_datagrid.datagrid("reload"); // 刷新
                        })
                    } else {
                        $.messager.alert("温馨提示", '保存失败', 'info', function () {
                        })
                    }
                }
            })
        },
        cancel: function () {
            client_dialog.dialog("close");
            exchange_dialog.dialog("close");
            developFailed_dialog.dialog("close");
            lost_dialog.dialog("close");
        },
        query: function () {
            var minInputDate = $("#minInputDate").textbox('getValue');
            var maxInputDate = $("#maxInputDate").textbox('getValue');
            var identitynum = $("#identitynum").textbox('getValue');
            var tel = $("#tel").textbox('getValue');
            var principal = $("#principal").textbox('getValue');
            var keyword = $("#keyword").textbox('getValue');
            $('#client_datagrid').datagrid('load', {
                minInputDate: minInputDate,
                maxInputDate: maxInputDate,
                identitynum: identitynum,
                tel: tel,
                principal: principal,//负责人
                keyword: keyword
            })
        },

        //移入资源池
        moveToPool: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return;
            }
            $.messager.confirm('确认', '你确认要操作吗？', function (r) {
                if (!r) return;
                $.get('/client/moveToPool.do', {id: row.id}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", '操作成功', 'info', function () {
                            client_datagrid.datagrid("reload");  // 刷新
                        })
                    } else {
                        $.messager.alert("温馨提示", '操作失败', 'info', function () {
                            client_datagrid.datagrid("reload");  // 刷新
                        })
                    }
                });
            });
        },
        //尝试开发
        exploit: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return;
            }
            $.messager.confirm('确认', '你确认要尝试开发该客户吗？', function (r) {
                if (!r) return;
                $.get('/client/exploit.do', {id: row.id}, function (data) {
                    if (data.success) {
                        $.messager.alert("温馨提示", '操作成功', 'info', function () {
                            client_datagrid.datagrid("reload");  // 刷新
                        })
                    } else {
                        $.messager.alert("温馨提示", '操作失败', 'info', function () {
                            client_datagrid.datagrid("reload");  // 刷新
                        })
                    }
                });
            });
        },
        //移交客户
        exchange: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return;
            }
            $.messager.confirm('确认', '你确认要移交该客户吗？', function (r) {
                if (!r) return;
                //弹框
                exchange_dialog.dialog("setTitle", "移交客户");
                exchange_dialog.dialog("open");

                //处理当前负责人回显
                if (row) {
                    row["client.name"] = row.name;
                    row["client.id"] = row.id;
                }

                //处理当前负责人回显
                if (row.chargeuser) {
                    row["oldcharge.username"] = row.chargeuser.username;
                    row["oldcharge.id"] = row.chargeuser.id;
                }

                if (row.newcharge) {
                    row["newcharge.id"] = row.newcharge.id;
                }

                // 回显数据
                exchange_dialog.form("load", row);
            });
        },
        //移交客户
        exchangeSave: function () {
            // 提交表单
            exchange_form.form("submit", {
                url: "/client/exchange.do",
                onSubmit: function (params) {
                    return exchange_form.form('validate');
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", '移交成功', 'info', function () {
                            client_datagrid.datagrid("reload");
                            objMethod.cancel();
                        })
                    } else {
                        $.messager.alert("温馨提示", '移交失败', 'info', function () {
                        })
                    }
                }
            })
        },
        //开发失败
        developFailed: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return;
            }
            $.messager.confirm('确认', '你确认要标记为开发失败客户吗？', function (r) {
                if (!r) return;
                //弹框
                developFailed_dialog.dialog("setTitle", "标记客户");
                developFailed_dialog.dialog("open");

                //处理当前用户回显
                if (row) {
                    row["client.name"] = row.name;
                    row["client.id"] = row.id;
                }

                //处理当前负责人回显
                if (row.chargeuser) {
                    row["charger.username"] = row.chargeuser.username;
                    row["charger.id"] = row.chargeuser.id;
                }

                // 回显数据
                developFailed_dialog.form("load", row);
            });
        },

        //标记开发失败用户提交表单
        developFailedSave: function () {
            // 提交表单
            developFailed_form.form("submit", {
                url: "/client/developFailed.do",
                onSubmit: function (params) {
                    return developFailed_form.form('validate');
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", '标记成功', 'info', function () {
                            client_datagrid.datagrid("reload");
                            objMethod.cancel();
                        })
                    } else {
                        $.messager.alert("温馨提示", '标记失败', 'info', function () {
                        })
                    }
                }
            })
        },
        //客户流失
        lost: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning');
                return;
            }
            $.messager.confirm('确认', '你确认要标记为流失客户吗？', function (r) {
                if (!r) return;
                //弹框
                lost_dialog.dialog("setTitle", "标记为流失客户");
                lost_dialog.dialog("open");

                //处理客户回显
                if (row) {
                    row["client.name"] = row.name;
                    row["client.id"] = row.id;
                }

                //处理当前负责人回显
                if (row.chargeuser) {
                    row["charger.username"] = row.chargeuser.username;
                    row["charger.id"] = row.chargeuser.id;
                }

                // 回显数据
                lost_dialog.form("load", row);
            });
        },
        //标记开发失败用户提交表单
        lostSave: function () {
            lost_form.form("submit", {
                url: "/client/lost.do",
                onSubmit: function (params) {
                    return lost_form.form('validate');
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", '标记成功', 'info', function () {
                            client_datagrid.datagrid("reload");
                            objMethod.cancel();
                        })
                    } else {
                        $.messager.alert("温馨提示", '标记失败', 'info', function () {
                        })
                    }
                }
            })
        },
        //刷新表单
        refresh: function () {
            client_datagrid.datagrid("reload");
        }

    };
    // 绑定事件
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
});

